package example3;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedReaderWriterTest {
    @Test
    public void read() {
        BufferedReader bufferedReader = null;
        try {
            FileReader fileReader = new FileReader("1.txt");
            bufferedReader = new BufferedReader(fileReader);

            char[] chars = new char[10];
            int num = 0;
            while (true) {
                num = bufferedReader.read(chars);
                if (num == -1) {
                    break;
                }
                String str = new String(chars, 0, num);
                System.out.print(str);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    @Test
    public void readLine() {
        BufferedReader bufferedReader = null;
        try {
            FileReader fileReader = new FileReader("1.txt");
            bufferedReader = new BufferedReader(fileReader);

            String str = null;
            while (true) {
                str = bufferedReader.readLine();
                if (str == null) {
                    break;
                }
                System.out.println(str);
            }
            

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }



    @Test
    public void readwriteLine() {
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            FileReader fileReader = new FileReader("1.txt");
            FileWriter fileWriter = new FileWriter("2.txt");

            bufferedReader = new BufferedReader(fileReader);
            bufferedWriter = new BufferedWriter(fileWriter);

            String str = null;

            while (true) {
                str = bufferedReader.readLine();
                if (str == null) {
                    break;
                }
                bufferedWriter.write(str);
                bufferedWriter.newLine();
                bufferedWriter.flush();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }




    @Test
    public void readwrite() {
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            FileReader fileReader = new FileReader("1.txt");
            FileWriter fileWriter = new FileWriter("2.txt");

            bufferedReader = new BufferedReader(fileReader);
            bufferedWriter = new BufferedWriter(fileWriter);

            char[] chars = new char[10];
            int num = 0;

            while ((num = bufferedReader.read(chars)) != -1) {
                bufferedWriter.write(chars, 0, num);
                bufferedWriter.flush();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }




}
















